﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class InventoryTypeDAO
    {
        DataServices m_TypeDAO = new DataServices();

        public DataTable GetAll()
        {
            m_TypeDAO.Load("GetAllInventoryType", null);
            return m_TypeDAO;
        }

        public DataTable GetById(String typeId)
        {
            SqlParameter param = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param.Value = typeId;

            m_TypeDAO.Load("GetInventoryTypeById", param);
            return m_TypeDAO;
        }

        public DataTable GetByLikeId(String typeId)
        {
            SqlParameter param = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param.Value = typeId;

            m_TypeDAO.Load("GetInventoryTypeByLikeId", param);
            return m_TypeDAO;
        }

        public DataTable GetByName(String typeName)
        {
            SqlParameter param = new SqlParameter("@TypeName", SqlDbType.NVarChar, 100);
            param.Value = typeName;

            m_TypeDAO.Load("GetInventoryTypeByName", param);
            return m_TypeDAO;
        }

        public int Add(InventoryTypeInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param[0].Value = info.TypeId;

            param[1] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 100);
            param[1].Value = info.TypeName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_TypeDAO.ExecuteUpdate("AddInventoryType", param);
        }

        public int Update(InventoryTypeInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param[0].Value = info.TypeId;

            param[1] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 100);
            param[1].Value = info.TypeName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_TypeDAO.ExecuteUpdate("UpdateInventoryType", param);
        }

        public int Delete(String typeId)
        {
            SqlParameter param = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param.Value = typeId;

            return m_TypeDAO.ExecuteUpdate("DeleteInventoryType", param);
        }

        public DataRow AddNewRow()
        {
            return m_TypeDAO.NewRow();
        }

        public void AddNewInventory(DataRow m_Row)
        {
            m_TypeDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_TypeDAO.ExecuteNoneQuery() > 0;
        }
    }
}
